/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package titanisu.basicMathModule1.fractionsCalculation;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Balaji
 */
public class QuestionGeneratorAddSub {

    ArrayList quest = new ArrayList();

    private void setQ(ArrayList arlst) {
        this.quest = arlst;
    }

    public ArrayList getQuest() {
        return quest;
    }

    public void setQuest(ArrayList quest) {
        this.quest = quest;
    }

    public String getQuestions() {

        int variables[] = new int[5];
        for (int i = 0; i < variables.length; i++) {
            variables[i] = (new Random()).nextInt(9) + 1;
        }

        String op = "";
        if (Math.random() > .5) {
            op = "+";
            quest.add(0, "<math>  <mfrac>       <mi> " + variables[0] + " </mi>  <mi> " + variables[1] + " </mi>    </mfrac>  <mo>  " + op + " </mo>   <mfrac>       <mi> " + variables[2] + " </mi>  <mi> " + variables[3] + " </mi>    </mfrac>  <mo> = </mo> <mi> ? </mi>  </math>  ");
            int[] an = new titanisu.mathUtil.NumberFetcher().simplifyFraction(variables[0] * variables[3] + variables[1] * variables[2], variables[3] * variables[1]);
            quest.add(1, "" + an[0]);
            quest.add(2, "" + an[1]);

        } else {
            op = "-";
            quest.add(0, "<math>  <mfrac>       <mi> " + variables[0] + " </mi>  <mi> " + variables[1] + " </mi>    </mfrac>  <mo>  " + op + " </mo>   <mfrac>       <mi> " + variables[2] + " </mi>  <mi> " + variables[3] + " </mi>    </mfrac>  <mo> = </mo> <mi> ? </mi>  </math>  ");
            int[] an = new titanisu.mathUtil.NumberFetcher().simplifyFraction(variables[0] * variables[3] - variables[1] * variables[2], variables[3] * variables[1]);
            quest.add(1, "" + an[0]);
            quest.add(2, "" + an[1]);
        }
        setQ(quest);
        System.out.println(" quest set in questionGenerator is " + quest);
        return (String) quest.get(0);
      }

    /**
     * Gives the answers
     *
     * @return
     */
    public String getAnswer() {
        String answer = "";
        String den = (String) quest.get(2);
        
        if (quest.size() > 2 && den != "1") {
            answer = " <math>  <mfrac>       <mi>" + quest.get(1) + "</mi>  <mi>" + quest.get(2) + "</mi>    </mfrac> </math>";
        } else {
            answer = " <math>  <mn> " + (String) quest.get(1) + "</mn> </math>";
        }
        return answer;
    }
}
